package com.global.college.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.global.college.R;

/**
 * 自定义dialog，含一行文本及两个按钮
 */
public class TwoLineDialog {

    private Dialog dialog;
    private TextView messageTv;
    private TextView tvTitle;
    private Button firstBtn;
    private Button secondBtn;
    private onButtnClickListener buttnClickListener;

    public TwoLineDialog(Context context) {
        initDialog(context);
    }

    public void setMessage(String messageTv) {
        if (null != messageTv && !messageTv.equals("")) {
            this.messageTv.setText(messageTv);
        }
    }

    public void setTitle(String title) {
        if (null != title && !title.equals("")) {
            this.tvTitle.setText(title);
        }
    }

    public void setButtonTitle(String text) {
        if (null != text && !text.equals("")) {
            this.firstBtn.setText(text);
        }
    }



    private void initDialog(Context context) {
        dialog = new Dialog(context);

        dialog.setContentView(R.layout.dialog_two_line);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        dialog.setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtils.getScreenWidth(context)
                - ScreenUtils.getScreenWidth(context) / 4;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        window.setWindowAnimations(R.style.dialog);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        messageTv = (TextView) dialog.findViewById(R.id.txt_dialog_message_2);
        tvTitle = (TextView) dialog.findViewById(R.id.txt_dialog_message_1);
        firstBtn = (Button) dialog.findViewById(R.id.btn_dialog_cancel);
        secondBtn = (Button) dialog.findViewById(R.id.btn_go_recharge);
        firstBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener){
                    buttnClickListener.onFirstBtnClick(v);
                }

            }
        });
        secondBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener){
                    buttnClickListener.onSecondBtnClick(v);
                }

            }
        });
    }

    public void setOneButton(){
        secondBtn.setVisibility(View.GONE);
        dialog.findViewById(R.id.dialog_bg).setVisibility(View.GONE);
    }

    public void setMessageColor(int color){
        messageTv.setTextColor(color);
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dissmissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
           
        }
    }

    public void setOnButtnClickListener(onButtnClickListener buttnClickListener) {
        this.buttnClickListener = buttnClickListener;
    }

    public interface onButtnClickListener {
        public void onFirstBtnClick(View v);

        public void onSecondBtnClick(View v);
    }

}